/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package model;

/**
 *
 * @author Stupi
 */
public class Determinant {
    
    public int determinantOfMatrix (int [][] matrix) {
       
		if (matrix.length == 1) return matrix[0][0];
		if (matrix.length == 2) return matrix[0][0]*matrix[1][1] - matrix[0][1]*matrix[1][0];

		int det = 0;
		int s;
		
		for (int i = 0; i < matrix.length; i++) {
			
			int [][] smallerMatrix = new int[matrix.length -1][matrix.length - 1]; 
			
			for (int a = 1; a < matrix.length; a++) {								
				for (int b = 0; b < matrix.length; b++) {
					
					if (b < i) smallerMatrix[a-1][b] = matrix[a][b];
					if (b > i) smallerMatrix[a-1][b-1] = matrix[a][b];
				}
			}
			
			if (i%2 == 0) s = 1;
			else s = -1;
			
			det += s*matrix[0][i]*determinantOfMatrix(smallerMatrix);
		}
		return det;
	}
    
}
